/*
 * Copyright 2008 Jens Meiss
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package mtt.marks;

import java.util.Enumeration;
import java.util.Vector;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;

import mtt.marks.editor.MarkEditorController;
import mtt.marks.editor.MarkEditorView;
import mtt.marks.editor.events.ShowMarkEditorViewEvent;
import mtt.marks.events.RefreshMarksViewEvent;
import mtt.marks.events.ShowMarksViewEvent;
import mtt.menu.events.ShowMainMenuEvent;
import mtt.rms.dataObjects.Mark;
import mtt.rms.events.recordstores.marks.events.RemoveMarkEvent;
import hmvc.j2me.events.IEvent;
import hmvc.j2me.events.TerminateEvent;
import hmvc.j2me.view.AView;

/**
 * 
 * 
 * @author jens.meiss
 */
public class MarksView extends AView {

	private List listMarks = new List("Notenübersicht", List.IMPLICIT);;

	private Command commandBack = new Command("zurück", Command.BACK, 1);;

	private Command commandAdd = new Command("Eintrag hinzufügen", Command.ITEM, 2);

	private Command commandEdit = new Command("Eintrag bearbeiten", Command.ITEM, 2);

	private Command commandDelete = new Command("Eintrag löschen", Command.ITEM, 2);

	private Vector vectorListEntries;

	public void commandAction(Command command, Displayable d) {

		if (command == commandBack) {

			getController().fireEventUp(new ShowMainMenuEvent());
			getController().fireEventLocal(new TerminateEvent());
		} else if (command == commandAdd) {

			getController().addChildComponent(new MarkEditorController(),
					new MarkEditorView());
			getController().fireEventDown(new ShowMarkEditorViewEvent());
			
		} else if (command == commandEdit) {

			int elementId = this.listMarks.getSelectedIndex();
			getController().addChildComponent(
					new MarkEditorController(),
					new MarkEditorView((Mark) vectorListEntries
							.elementAt(elementId)));
			getController().fireEventDown(new ShowMarkEditorViewEvent());

		} else if (command == commandDelete) {

			int elementId = this.listMarks.getSelectedIndex();
			Mark mark = (Mark) vectorListEntries.elementAt(elementId);

			getController().fireEventUp(new RemoveMarkEvent(mark));
			getController().fireEventLocal(new ShowMarksViewEvent());

		}

	}

	public void handleEvent(IEvent event) {

		if (event instanceof RefreshMarksViewEvent) {
			refreshView((Vector) event.getPayload());
		}
	}

	public void init() {
	}

	/**
	 * refreshes the view with the existing entries
	 * 
	 * @param vectorMarks
	 */
	private void refreshView(Vector vectorMarks) {

		/* 
		 * TDOO: prüfen ob es überhaupt hinzugefügt wurde 
		 */
		this.listMarks.removeCommand(commandAdd);
		this.listMarks.removeCommand(commandBack);
		this.listMarks.removeCommand(commandDelete);
		this.listMarks.removeCommand(commandEdit);
		
		this.listMarks.deleteAll();

		/*
		 * sind Einträge vorhanden
		 */
		if ((vectorMarks == null) || vectorMarks.size() == 0) {

			this.listMarks.append("Keine Noten eingetragen", null);
			
			if (vectorMarks == null) {
				/*
				 * leeren Vector erstellen
				 */
				vectorMarks = new Vector(3);
			}
			
		} else {
			
			this.listMarks.addCommand(commandEdit);
			this.listMarks.addCommand(commandDelete);
			
		} 
		
		/*
		 * add standard commands
		 */
		this.listMarks.addCommand(commandBack);
		this.listMarks.addCommand(commandAdd);
		

		/*
		 * Vector sichern
		 */
		this.vectorListEntries = vectorMarks;

		/*
		 * Einträge hinzufügen
		 */
		Enumeration enumeration = vectorMarks.elements();
		while (enumeration.hasMoreElements()) {
			Mark mark = (Mark) enumeration.nextElement();
			this.listMarks.append("Name: " + mark.getName() + " Note: "
					+ mark.getMark(), null);
		}
	}

	public void registerEvents() {
		
		getController().register(RefreshMarksViewEvent.class, this);
		
		this.listMarks.addCommand(commandBack);

		setDisplayAble(this.listMarks);
	}

	public void terminate() {
		// TODO Auto-generated method stub

	}

}
